package com.diy.common.bussiness.controller;

import com.alibaba.fastjson.JSON;
import com.diy.common.annontation.TableInfo;
import com.diy.common.baseModel.BaseDiyBean;
import com.diy.common.baseModel.BaseDiyQueryParamsBean;
import com.diy.common.baseModel.DiyResponse;
import com.diy.common.bussiness.service.defaultService.DefaultSaveable;
import com.diy.common.exception.ProgramException;
import com.diy.common.helper.InstanceHelper;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * Created by GY on 2015/11/21.
 */
public abstract class BaseManagerController<T extends BaseDiyBean,P extends BaseDiyQueryParamsBean,R> extends BaseController {


    //默认增删改的初始方法
    private DefaultSaveable<T, P> defaultSaveable;

    //获取查询类的信息
    private Class<P> queryOfClass;

    static String mapperName;

    protected BaseManagerController(DefaultSaveable<T, P> defaultSaveable, Class<T> responseClass, Class<P> queryClass) {

        this.defaultSaveable = defaultSaveable;
        this.queryOfClass = queryClass;

        TableInfo getTableInfo = responseClass.getAnnotation(TableInfo.class);

        if (getTableInfo != null) {
            mapperName = getTableInfo.MapperName();
        }
    }

    //除单个获取资源时，将查询的表进行补值
    private final void setQueryTableName(P searchParams) {
        if (searchParams != null)
            searchParams.setMapperName(mapperName);
    }


    //单个查询时的应用，将参数进行进一步封装
    protected P changeQueryParameters(P setSearchWhere, Object vl) {

        setSearchWhere.setId(null);
        try {
            if (vl!=null)
            {
                setQueryTableName(setSearchWhere);
                setSearchWhere.setId(Long.valueOf(String.valueOf(vl)));
            }
        }
        catch (Exception ex)
        {
            throw new ProgramException("单个资源查询产生异常"+"msg:"+ex.getMessage(),2001);
        }
        return setSearchWhere;
    }


    /**
     *  创建资源
     * @param arg
     * @param diyResponse
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public
    @ResponseBody
    DiyResponse create(@RequestBody P arg, DiyResponse diyResponse) {

        setQueryTableName(arg);
        System.out.println("controller     " + JSON.toJSONString(arg));
        T result = defaultSaveable.select(arg);
        return diyResponse.success(arg);
    }

    @RequestMapping( method = RequestMethod.POST)
    public
    @ResponseBody
    DiyResponse edit(@RequestBody P arg, DiyResponse diyResponse) {

        setQueryTableName(arg);

        System.out.println("controller     " + JSON.toJSONString(arg));
        T result = defaultSaveable.select(arg);

        return diyResponse.success(arg);
    }


    @RequestMapping(method = RequestMethod.DELETE)
    public
    @ResponseBody
    DiyResponse delete(@RequestBody P arg, DiyResponse diyResponse) {
        setQueryTableName(arg);
        System.out.println("controller     " + JSON.toJSONString(arg));
        T result = defaultSaveable.select(arg);

        return diyResponse.success(arg);
    }



    /**
     * 单个查询应用
     * @param arg 查询指定资源Key，默认存储Id为Integer形，需要指定其它字段可以重写（compileGetObject）进行处理
     * @param diyResponse  默认输出对象
     * @return
     */
    @RequestMapping(value = "/{objectKey}", method = RequestMethod.GET)
    public
    @ResponseBody
    DiyResponse single(@PathVariable("objectKey") String arg, DiyResponse diyResponse) {

        //进行封装查询参数，调其转换为对象
        P currentQueryParams = InstanceHelper.createInstance(queryOfClass);

        currentQueryParams = changeQueryParameters(currentQueryParams, arg);

        System.out.println("controller     " + JSON.toJSONString(arg));

        T result = defaultSaveable.select(currentQueryParams);

        return diyResponse.success(currentQueryParams);
    }


    /**
     * 批量查询应用
     * @param arg
     * @param diyResponse
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public
    @ResponseBody
    DiyResponse query(@RequestBody P arg, DiyResponse diyResponse) {
        setQueryTableName(arg);
        System.out.println("controller     " + JSON.toJSONString(arg));
        T result = defaultSaveable.select(arg);

        return diyResponse.success(result);
    }

}

